﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

namespace GrayParrot.ServiceBus
{
    [Obsolete("Classe di test. Non utilizzare...")]
    public static class Messaging
    {
        private const string Issuer = "owner";
        private const string Key = "cq0gEqtO/XLpWlyYlRwgVPvj/h0+dKgYA8i0gvvcZzM=";
        private const string Namespace = "testgdep5";
        public const string Topic = "";
        public const string Endpoint = "Endpoint=sb://testgdep5.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=cq0gEqtO/XLpWlyYlRwgVPvj/h0+dKgYA8i0gvvcZzM=";

        public static MessagingFactory GetMessagingFactory()
        {
            var m_tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(Issuer, Key);
            var m_uri = ServiceBusEnvironment.CreateServiceUri("sb", Namespace, string.Empty);
            return MessagingFactory.Create(m_uri, m_tokenProvider);
        }

        public static NamespaceManager GetNamespaceManager()
        {
            var m_tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(Issuer, Key);
            var m_uri = ServiceBusEnvironment.CreateServiceUri("sb", Namespace, string.Empty);
            return new NamespaceManager(m_uri, m_tokenProvider);
        }

        public static void SendMessage(string destination, BrokeredMessage message)
        {
            //var msgF = MessagingFactory.Create(Endpoint);
            //var sender = msgF.CreateMessageSender("ProposteMgr");
            //sender.Send(message);

            TopicClient Client = TopicClient.CreateFromConnectionString(Endpoint, destination);
            Client.Send(message);
        }

        public static SubscriptionClient CreateSubscription(string topic, string subscription)
        {
            return SubscriptionClient.CreateFromConnectionString(Endpoint, topic, subscription);
        }
    }
}
